﻿using System.Xml.Linq;
using Microsoft.Xna.Framework;

namespace BattleFury.Lib.Extensions
{
	public static class XmlExtensions
	{
		public static string GetString(this XElement element, string attributeName)
		{
			var attrib = element.Attribute(attributeName);
			if (attrib == null) return string.Empty;
			var value = attrib.Value;
			if (string.IsNullOrEmpty(value)) return string.Empty;
			return value;
		}

		public static string GetString(this XElement element, string attributeName, string defaultValue)
		{
			var attrib = element.Attribute(attributeName);
			if (attrib == null) return defaultValue;
			var value = attrib.Value;
			if (string.IsNullOrEmpty(value)) return defaultValue;
			return value;
		}

		public static bool GetBoolean(this XElement element, string attributeName)
		{
			var attrib = element.Attribute(attributeName);
			if (attrib == null) return false;
			bool value;
			bool.TryParse(attrib.Value, out value);
			return value;
		}

		public static bool GetBoolean(this XElement element, string attributeName, bool defaultValue)
		{
			var attrib = element.Attribute(attributeName);
			if (attrib == null) return defaultValue;
			bool value;
			bool.TryParse(attrib.Value, out value);
			return value;
		}

		public static Color GetColor(this XElement element, string attributeName)
		{
			var attrib = element.Attribute(attributeName);
			if (attrib == null) return Color.White;
			var value = new Color().FromHex(attrib.Value);
			return value;
		}

		public static Color GetColor(this XElement element, string attributeName, Color defaultColor)
		{
			var attrib = element.Attribute(attributeName);
			if (attrib == null) return defaultColor;
			var value = new Color().FromHex(attrib.Value);
			return value;
		}

		public static int GetInt(this XElement element, string attributeName)
		{
			var attrib = element.Attribute(attributeName);
			if (attrib == null) return 0;
			int value;
			int.TryParse(attrib.Value, out value);
			return value;
		}

		public static int GetInt(this XElement element, string attributeName, int defaultValue)
		{
			var attrib = element.Attribute(attributeName);
			if (attrib == null) return defaultValue;
			int value;
			int.TryParse(attrib.Value, out value);
			return value;
		}

		public static float GetFloat(this XElement element, string attributeName)
		{
			var attrib = element.Attribute(attributeName);
			if (attrib == null) return 0f;
			float value;
			float.TryParse(attrib.Value, out value);
			return value;
		}

		public static float GetFloat(this XElement element, string attributeName, float defaultValue)
		{
			var attrib = element.Attribute(attributeName);
			if (attrib == null) return defaultValue;
			float value;
			float.TryParse(attrib.Value, out value);
			return value;
		}

		public static double GetDouble(this XElement element, string attributeName)
		{
			var attrib = element.Attribute(attributeName);
			if (attrib == null) return 0.0;
			double value;
			double.TryParse(attrib.Value, out value);
			return value;
		}

		public static double GetDouble(this XElement element, string attributeName, double defaultValue)
		{
			var attrib = element.Attribute(attributeName);
			if (attrib == null) return defaultValue;
			double value;
			double.TryParse(attrib.Value, out value);
			return value;
		}
	}
}